package com.xgr.wonderful;


import com.xgr.wonderful.adapter.NavigationAdapter;
import com.xgr.wonderful.fragment.Mainfragment;
import com.xgr.wonderful.fragment.MyInfoFragment;
import com.xgr.wonderful.fragment.PersonalCenterFragment;
import com.xgr.wonderful.fragment.RepliedFragment;
import com.xgr.wonderful.fragment.SettingsFragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class NaviFragment extends Fragment {
	
	public static final int USERINFO= 0;
	public static final int HOME = 1;
	public static final int PERSONALCENTER = 2;
	public static final int REPLIED= 3;
	public static final int SETTINGS = 4;
	
	private static final String TAG ="NaviFragment" ;
	
	private MainActivity mActivity ;
	
	private int currentFragmentId = HOME; //默认情况下是1 
	
	private MyInfoFragment myInfoFragment ;
	private Mainfragment mMainFragment;
	private PersonalCenterFragment mPersonalCenterFragment;
	private RepliedFragment mRepliedFragment;
	private SettingsFragment mSettingsFragment ;
	
	private FragmentManager fragmentManager ;
	
	private NavigationAdapter navigationAdapter ;
	
	ListView mNaviListView ;
	String [] mTitles ;
	Integer [] mDrawerIds ;
	
	private View rootView;// 缓存Fragment view
	
	@Override
	public void onAttach(Activity activity) {
		mActivity = (MainActivity) activity;
		super.onAttach(activity);
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (rootView == null){
			rootView = inflater.inflate(R.layout.fragment_navi, null);
		}
		fragmentManager = getFragmentManager() ;
		init() ;
		return rootView ;
	}
	
	private void init (){
		mNaviListView = (ListView)rootView.findViewById(R.id.navi_listview) ;
		getData() ;
		navigationAdapter = new NavigationAdapter(getActivity(),
				mTitles, mDrawerIds) ;
		mNaviListView.setAdapter(navigationAdapter) ;
		mNaviListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				//选择
				onTabSelected( position) ;
				//关闭slidingmenu
				mActivity.getSlidingMenu().toggle() ;
			}
		}) ;
		onTabSelected(currentFragmentId) ;
	}
	
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		refresh() ;
		super.onResume();
	}
	
	private void refresh (){
		//本来是用在改名后的，但是好像也没有人会常常改名，这里反而有时会有问题
		//navigationAdapter.notifyDataSetChanged() ;
	}
	
	private void getData (){
		//一共6个
		mTitles = getResources().getStringArray(R.array.navigations) ;
		mDrawerIds = new Integer[] 
				{R.drawable.ic_navi_home2,
				R.drawable.ic_navi_me2,
				R.drawable.ic_navi_myreply2,
				R.drawable.ic_navi_settings2};
	}
	
	/**
	 * 注意标题的变化 
	 * @param index
	 */
	private void onTabSelected (int index){
		FragmentTransaction transaction = fragmentManager.beginTransaction() ;
		
		switch (index) {
		case USERINFO:
			hideFragments(transaction) ;
			if (null == myInfoFragment){
				myInfoFragment = new MyInfoFragment() ;
				//center在frame_center中
				transaction.add(R.id.center, myInfoFragment) ;
			}else {
				transaction.show(myInfoFragment) ;
			}
			currentFragmentId = USERINFO ;
			mActivity.onNaviItemSelected(USERINFO) ;
			break;
		case HOME:
			hideFragments(transaction) ;
			if (null == mMainFragment){
				mMainFragment = new Mainfragment() ;
				//center在frame_center中
				transaction.add(R.id.center, mMainFragment) ;
			}else {
				transaction.show(mMainFragment) ;
			}
			currentFragmentId = HOME ;
			mActivity.onNaviItemSelected(HOME) ;
			break ;
		case PERSONALCENTER:
			hideFragments(transaction) ;
			if (null == mPersonalCenterFragment){
				mPersonalCenterFragment = new PersonalCenterFragment() ;
				transaction.add(R.id.center, mPersonalCenterFragment) ;
			}else {
				transaction.show(mPersonalCenterFragment) ;
			}
			currentFragmentId = PERSONALCENTER ;
			mActivity.onNaviItemSelected(PERSONALCENTER) ;
			break ;
		case REPLIED:
			hideFragments(transaction) ;
			if (null == mRepliedFragment){
				mRepliedFragment = new RepliedFragment() ;
				transaction.add(R.id.center, mRepliedFragment) ;
			}else {
				transaction.show(mRepliedFragment) ;
			}
			currentFragmentId = REPLIED ;
			mActivity.onNaviItemSelected(REPLIED) ;
			break ;
		case SETTINGS:
			hideFragments(transaction) ;
			if (null == mSettingsFragment){
				mSettingsFragment = new SettingsFragment() ;
				transaction.add(R.id.center, mSettingsFragment) ;
			}else {
				transaction.show(mSettingsFragment) ;
			}
			currentFragmentId = SETTINGS ;
			mActivity.onNaviItemSelected(SETTINGS) ;
			break ;
		default:
			break;
		}
		transaction.commit() ;
	}
	
	private void hideFragments( FragmentTransaction transaction){
		if (myInfoFragment!= null){
			transaction.hide(myInfoFragment) ;
		}
		if (mMainFragment != null){
			transaction.hide(mMainFragment) ;
		}
		if (mPersonalCenterFragment != null) {
			transaction.hide(mPersonalCenterFragment);
		}
		if (mRepliedFragment != null) {
			transaction.hide(mRepliedFragment);
		}	
		if (mSettingsFragment != null){
			transaction.hide(mSettingsFragment) ;
		}
	}
	
	
	public int getCurrentFragmentId (){
		return currentFragmentId ;
	}
	
	public void setCurrentFragmentId ( int index ){
		this.currentFragmentId = index ;
	}

	
	
	
}
